package com.sinhvien.ui;


import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.sinhvien.model.DBHander;
import com.sinhvien.vo.contact;

public class MainActivity extends Activity {


    DBHander db;


    EditText editId;

    EditText editName;

    EditText editPhone;

    Button btnAdd;

    Button btnGetAll;


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        db = new DBHander(this);


        editId = (EditText)findViewById(R.id.editId);

        editName = (EditText)findViewById(R.id.editName);

        editPhone = (EditText)findViewById(R.id.editPhone);

        btnAdd = (Button)findViewById(R.id.btnAdd);

        btnGetAll = (Button)findViewById(R.id.btnGetAll);


        btnAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                contact contact = new contact(Integer.parseInt(editId.getText().toString()),
                                 editName.getText().toString(), editPhone.getText().toString());
                db.addNewContact(contact);
                System.out.println("Add a new contact:");
                System.out.println(db.getContact(contact.id).id + "---" + db.getContact(contact.id).name);

            }
        });


              //  contact contact = new contact(Integer.parseInt(editId.getText().toString()),
               //         editName.getText().toString(), editPhone.getText().toString());

              //  db.addNewContact(contact);
              //  System.out.println("Add a new contact:");
             //   System.out.println(db.getContact(contact.id).id + "---" + db.getContact(contact.id).name);




        btnGetAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                List<contact> listContact = db.getAllContact();
                System.out.println("All the contacts:");
                for(int i=0; i<listContact.size(); i++){
                    System.out.println(db.getContact(listContact.get(i).id).id + "---" + db.getContact(listContact.get(i).id).name
                            + "---" + listContact.get(i).phone);
                }
            }
        });

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}


